from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
import time
import unittest
from twohomeweek.Mysql import Mysql
# 定义一个类名称
class A3mall1(unittest.TestCase):
    def setUp(self):
        # 环境
        self.driver = webdriver.Edge()
        self.driver.get('http://10.59.9.14:7003/')
        self.db = Mysql(host='10.59.9.14',user='root',password='yidong666',database='a3mall')
    def tearDown(self):
        time.sleep(10)
        self.driver.quit()
    def test_01_login(self):
        # 点击登录页面 使用xpath定位方式
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        # 输入账号，  使用class定位方式
        self.driver.find_element(By.CLASS_NAME,'regi-input').send_keys('15232008902')
        # 输入密码，   使用css定位方式
        self.driver.find_element(By.NAME,'password').send_keys('123456')
        time.sleep(15)
        #点击登录按钮
        self.driver.find_element(By.CLASS_NAME,'finish-btn').click()
        time.sleep(2)
        # 查询数据库
        ku1 = self.db.query('select * from mall_users where mobile = 15232008902')
        print(ku1)
        time.sleep(2)
        # 定位余额
        dy1 = self.driver.find_element(By.CLASS_NAME, 'color-black').text
        a = dy1.split()[1]
        print(dy1.split()[1])
        # 数据库查询余额
        shudy1 = self.db.query('select amount from mall_users where mobile = 15232008902')
        print(shudy1[0][0])
        self.assertEqual(float(shudy1[0][0]), float(a))
        # 定位积分
        dy2 = self.driver.find_element(By.CLASS_NAME, 'color-orage').text
        a2 = dy2.split()[0]
        print(a2)
        # 数据库查询积分
        shudy2 = self.db.query("select point from mall_users where mobile = 15232008902")
        print(shudy2)
        self.assertEqual(float(shudy2[0][0]), float(a2))
    def test_02_business(self):
        # 调用登录
        self.test_01_login()

        # 点击首页
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/ul/li[1]/a').click()
        # 断言进入商品页面
        dy3=self.driver.find_element(By.CLASS_NAME,'categories-title').text
        print(dy3)
        assert dy3 in '全部商品分类'
        # 向下滑动400
        self.driver.execute_script('window.scrollTo(0,400)')
        # 点击商品巴雷特
        self.driver.find_element(By.CLASS_NAME,'lazy').click()
        time.sleep(2)
        # 断言进入商品详情
        dy4=self.driver.find_element(By.XPATH,'/html/body/div[4]/a[3]').text
        print(dy4)
        assert dy4 in "巴雷特 M82A1 狙击枪"
        dy5= self.driver.find_element(By.ID,'sell-price-box').text
        print(dy5)
        # 数据库断言商品价格
        shudy3 =self.db.query('select sell_price from mall_goods where title = "巴雷特 M82A1 狙击枪"')
        print(shudy3)
        self.assertEqual(float(shudy3[0][0]),float(dy5))
        # 点击购买
        self.driver.find_element(By.ID,'buy-cart').click()
        time.sleep(2)
        # 确认订单页面向下滑动500
        self.driver.execute_script('window.scrollTo(0,500)')
        # 定位确认订单
        dy6=self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
        print(dy6)
        assert dy6 in '确认订单'
        # 断言支付价格
        dy7=self.driver.find_element(By.XPATH,'/html/body/div[4]/form/div[4]/div[2]/div[2]/div/div/div[2]/span[2]').text
        print(dy7)
        # 断言赠送积分
        dy8 = self.driver.find_element(By.XPATH,'/html/body/div[4]/form/div[4]/div[2]/div[1]/div[2]/div[2]/span').text
        print(dy8)
        # 下一步
        self.driver.find_element(By.CLASS_NAME,'ask-btn').click()
        time.sleep(2)
        # 断言送货地址
        dy11 = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[5]/div[2]/ul/li[3]').text
        print(dy11)
        shu4 = self.db.query("select address from mall_users_address where mobile = 15232008902")
        print(shu4)
        self.assertIn(shu4[0][0],dy11)
        # 断言订单完成页面
        dy9 = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
        print(dy9)
        assert dy9 in '订单完成'
        # 断言订单完成
        dy10 = self.driver.find_element(By.CLASS_NAME, 'succe-ico').text
        print(dy10)
        assert dy10 in '您的订单支付成功'
        dy12=self.driver.find_element(By.XPATH,'/html/body/div[4]/div[4]/div[2]/ul/li[1]').text
        print(dy12)
        # 查看订单
        self.driver.find_element(By.CLASS_NAME, 'immediate-pay').click()
        dy11 = self.driver.find_element(By.XPATH, '/html/body/div[4]/a[3]').text
        print(dy11)
        assert dy11 == '我的订单'
        dy13 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
        print(dy13)
        assert dy13 in dy12
    def test_03_business(self):
        self.test_01_login()
        # 点击首页
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/ul/li[1]/a').click()
        time.sleep(2)
        # 断言
        dy1 = self.driver.find_element(By.CLASS_NAME,'categories-title').text
        print(dy1)
        assert dy1 in "全部商品分类"
        # 定位搜索框
        search = self.driver.find_element(By.CLASS_NAME, 'search-input')
        search.send_keys('老太')
        search.send_keys(Keys.ENTER)
        time.sleep(2)
        # 定位搜索结果
        dy2 = self.driver.find_element(By.XPATH,'/html/body/div[4]/a[2]').text
        print(dy2)
        assert dy2 in '搜索结果'
        self.driver.find_element(By.CLASS_NAME,'lazy').click()
        time.sleep(2)
        # 定位已经到了商品页面
        dy3 = self.driver.find_element(By.XPATH,'/html/body/div[4]/a[3]').text
        print(dy3)
        assert dy3=='老太'
        # 断言商品价格
        dy4 = self.driver.find_element(By.ID,'sell-price-box').text
        print(dy4)
        shudy1 =self.db.query('select sell_price from mall_goods where title = "老太"')
        print(shudy1)
        self.assertEqual(float(shudy1[0][0]),float(dy4))
        # 加入购物车
        self.driver.find_element(By.ID,'add-cart').click()
        # 进入购物车
        self.driver.find_element(By.CLASS_NAME,'nav-settcart').click()
        # 断言进入购物车页面
        dy7=self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
        print(dy7)
        assert dy7 in '我的购物车'

        # 断言购物车商品名称
        dy5=self.driver.find_element(By.XPATH,'/html/body/div[4]/form/div[1]/div[1]/table/tbody/tr[2]/td[3]/a/p').text
        print(dy5)
        assert dy5 == dy3
        dy6 = self.driver.find_element(By.XPATH,'/html/body/div[4]/form/div[1]/div[1]/table/tbody/tr[2]/td[5]').text
        print(dy6)
        assert dy6[1] in dy4
























if __name__ == '__main__':
    unittest.main('')
